package com.woniuxy.redis.client;


import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.Socket;

/**
 * @Author: 马宇航
 * @Do: 模拟一个假的jedis，让它往真正的redis服务器写数据
 * @DateTime: 22/05/26/0026 下午 4:25
 * @Component: 成都蜗牛学苑
 **/
public class JedisFaker {
    Socket socket;

    public JedisFaker(String url,int port) {
        try {
            this.socket = new Socket("121.4.62.111",6379);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void set(String key,String value){
        StringBuffer sb = new StringBuffer();
        sb.append("*3").append("\r\n");
        sb.append("$3").append("\r\n");
        sb.append("SET").append("\r\n");
        sb.append("$").append(key.length()).append("\r\n");
        sb.append(key).append("\r\n");
        sb.append("$").append(value.length()).append("\r\n");
        sb.append(value).append("\r\n");
        byte [] bytes = new byte[0];
        try {
            bytes = sb.toString().getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        OutputStream outputStream ;
        try {
            outputStream = socket.getOutputStream();
            outputStream.write(bytes);
//            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
